/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.manage.repository.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.customer.core.domain.operator.CustomerOperator;
import info.peigen.hotpot.business.customer.core.repository.entity.CustomerOperatorEntity;
import info.peigen.hotpot.business.customer.manage.convertor.CustomerOperatorManageConvertor;
import info.peigen.hotpot.business.customer.manage.repository.mapper.CustomerOperatorManageMapper;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import org.noear.solon.annotation.Component;

/**
 * <b>(CustomerOperatorManageRepository)</b>
 * 管理后台 "客户操作员" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023-09-08
 */
@SuppressWarnings("unused")
@Component
public class CustomerOperatorManageRepository extends AbstractBeetlSqlRepository<CustomerOperatorManageMapper, CustomerOperatorEntity> {
    public CustomerOperator active(String operatorId) {
        Assert.isTrue(StrUtil.isNotEmpty(operatorId), () -> new BusinessException(ResultCode.FAILURE, "激活时[operatorId]不得为空!"));

        CustomerOperatorEntity entity = getMapper().findByOperatorId(operatorId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!operatorId=" + operatorId));

        return CustomerOperatorManageConvertor.convertToDomain(entity);
    }

    public CustomerOperator lock(String operatorId) {
        Assert.isTrue(StrUtil.isNotEmpty(operatorId), () -> new BusinessException(ResultCode.FAILURE, "加锁时[operatorId]不得为空!"));

        CustomerOperatorEntity entity = getMapper().lockByOperatorId(operatorId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!operatorId为空!"));

        return CustomerOperatorManageConvertor.convertToDomain(entity);
    }

    public void removeByOperatorId(String operatorId) {
        Assert.isTrue(StrUtil.isNotEmpty(operatorId), () -> new BusinessException(ResultCode.FAILURE, "删除时[operatorId]不得为空!"));

        getMapper().deleteByOperatorId(operatorId);
    }

    public void store(CustomerOperator domain) {
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));

        CustomerOperatorEntity entity = CustomerOperatorManageConvertor.convertToEntity(domain);

        saveOrUpdate(entity);
    }
}